package com.leolian.base;

import org.junit.After;
import org.junit.Before;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.util.StringUtils;

public class UnitTestBase {
	private String pathContext;
	private ClassPathXmlApplicationContext context;
	
	public UnitTestBase(){}
	
	public UnitTestBase(String path){
		this.pathContext = path;
	}
	
	@Before
	public void before(){
		if(StringUtils.isEmpty(pathContext))
			pathContext = "classpath*:spring-*.xml";
		context = new ClassPathXmlApplicationContext(pathContext);
		context.start();
	}
	
	@After
	public void after(){
		context.destroy();
	}
	
	@SuppressWarnings("unchecked")
	protected <T extends Object> T getBean(String beanId){
		return (T) context.getBean(beanId);
	}
	
	protected <T extends Object> T getBean(Class<T> clazz){
		return context.getBean(clazz);
	}
	
}
